<template>
  <el-dialog :title="title" top="100px" width="600px" append-to-body :visible.sync="dialogedit" @open="openDialog"
    :before-close="closeDialog" :close-on-click-modal="false">
    <div class="form-box">
      <el-form ref="formdata" :model="ruleform" :rules="rules" label-suffix="：" :validate-on-rule-change="false"
        :show-message="false" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="岗位编码" prop="post_code"><el-input
                v-model="ruleform.post_code"></el-input></el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="岗位名称" prop="post_name"><el-input
                v-model="ruleform.post_name"></el-input></el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" :rows="5" v-model="ruleform.remark"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态" prop="status">
              <el-radio v-model="ruleform.status" label="0">正常</el-radio>
              <el-radio v-model="ruleform.status" label="1">停用</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="save_validate()">保 存</el-button>
      <el-button @click="closeDialog">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import {
    savePost
  } from '@/api/system/post';
  export default {
    name: 'post_edit',
    props: {
      dialogedit: {
        type: Boolean,
        default: false
      },
      ruleform: {
        type: Object,
        default: () => {
          return {
            post_id: 0,
            post_code: '',
            post_name: '',
            status: '',
            remark: ''
          };
        }
      }
    },
    data() {
      return {
        title: '修改岗位',
        rules: {
          post_code: [{
            required: true,
            message: '岗位编码不能为空'
          }],
          post_name: [{
            required: true,
            message: '岗位名称不能为空'
          }],
          status: [{
            required: true,
            message: '状态不能为空'
          }]
        }
      };
    },
    created() {},
    methods: {
      closeDialog() {
        this.$nextTick(() => {
          this.$refs.formdata.resetFields();
          this.$emit('update:dialogedit', !this.dialogedit);
        });
      },
      save_validate() {
        this.$refs.formdata.validate((valid, valids) => {
          if (valid) {
            this.save_post();
          } else {
            var msg = '';
            for (var key in valids) {
              var item = valids[key];
              msg = item[0].message;
              break;
            }
            if (msg) this.$message.error(msg);

            return false;
          }
        });
      },

      async save_post() {
        const loading = this.loading('保存中...');
console.log(this.ruleform);
        savePost(this.ruleform).then(response => {
          this.$modal.msgSuccess("保存成功");
          loading.close();
          this.closeDialog();
          this.$emit('reload');
        }).catch(err => {
          loading.close();
        });
      },
      async openDialog() {
        if (this.ruleform.post_id == 0) {
          this.title = "新增岗位";
        } else {
          this.title = "修改岗位";
        }
      }
    }
  };
</script>

<style scoped>
  .form-box {
    min-height: 60px;
    padding-right: 10px;
    overflow: auto;
  }
</style>
